package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.SelectController;
import org.testis.gui.mvc.utilities.MyTreeRenderer;
import java.beans.PropertyChangeEvent;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.util.Enumeration;
import javax.swing.JDialog;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Cahit Emir Erel
 */
public class SelectView extends AtofBaseDialog implements AbstractViewPanel {

    private static final long serialVersionUID = 74582374L;
    private MyDefaultMutableTreeNode m_jDesignTreeRoot;
    private SelectController m_controller;

    public SelectView(Frame parent,
            boolean modal,
            MyDefaultMutableTreeNode root,
            SelectController controller) {
        super(parent,
                modal);
        m_controller = controller;
        m_jDesignTreeRoot = root;
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrpFilter = new javax.swing.ButtonGroup();
        splitPane = new javax.swing.JSplitPane();
        leftPanel = new javax.swing.JPanel();
        treeScrollPane = new javax.swing.JScrollPane();
        tree = new JTree(m_jDesignTreeRoot);
        rightPanel = new javax.swing.JPanel();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        propertyScrollPane = new javax.swing.JScrollPane();
        propertyPane = new javax.swing.JTextPane();
        selectedNodeNumberLabel = new javax.swing.JLabel();
        selectAllCheckBox = new javax.swing.JCheckBox();
        rbShowAll = new javax.swing.JRadioButton();
        rbSameParents = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Selection");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        splitPane.setDividerLocation(600);

        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.putClientProperty("JTree.lineStyle", "Angled");
        tree.setCellRenderer(new MyTreeRenderer());
        tree.setExpandsSelectedPaths(false);
        tree.setRootVisible(false);
        tree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeMouseClicked(evt);
            }
        });
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        treeScrollPane.setViewportView(tree);

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addComponent(treeScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(treeScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
        );

        splitPane.setLeftComponent(leftPanel);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        propertyPane.setEditable(false);
        propertyScrollPane.setViewportView(propertyPane);

        selectAllCheckBox.setText("Select All");
        selectAllCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllCheckBoxActionPerformed(evt);
            }
        });

        btnGrpFilter.add(rbShowAll);
        rbShowAll.setSelected(true);
        rbShowAll.setText("Show All");
        rbShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbShowAllActionPerformed(evt);
            }
        });

        btnGrpFilter.add(rbSameParents);
        rbSameParents.setText("Just Show Same Parents");
        rbSameParents.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSameParentsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
        rightPanel.setLayout(rightPanelLayout);
        rightPanelLayout.setHorizontalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(rightPanelLayout.createSequentialGroup()
                        .addComponent(rbShowAll)
                        .addGap(2, 2, 2)
                        .addComponent(rbSameParents))
                    .addGroup(rightPanelLayout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectAllCheckBox)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectedNodeNumberLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addGap(156, 156, 156))
            .addComponent(propertyScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
        );
        rightPanelLayout.setVerticalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, rightPanelLayout.createSequentialGroup()
                .addComponent(propertyScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(selectedNodeNumberLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                    .addGroup(rightPanelLayout.createSequentialGroup()
                        .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbSameParents)
                            .addComponent(rbShowAll))
                        .addGap(18, 18, 18)
                        .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(okButton)
                            .addComponent(cancelButton)
                            .addComponent(selectAllCheckBox))))
                .addGap(13, 13, 13))
        );

        splitPane.setRightComponent(rightPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1041, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

//    private int mouseX;
//    private int mouseY;
//    private Component component;
    private void treeMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_treeMouseClicked
    {//GEN-HEADEREND:event_treeMouseClicked

        MyDefaultMutableTreeNode node = getLastSelectedPathComponent();
        int x = evt.getX();
        int y = evt.getY();
        int row = tree.getRowForLocation(x,
                y);
        TreePath path = tree.getPathForRow(row);
        if (path != null && node != null) {
            if (evt.getX() > tree.getPathBounds(path).x + 21) {
                return;
            }
            boolean isSelected = !(node.isSelected());
            if (node.hasCheckBox()) {
                if (node.isCheckBoxSelectable()) {
                    m_controller.operationCheckboxClicked(node, isSelected);
                    if (isSelected) {
                        tree.expandPath(path);
                        tree.repaint();
                    } else {
                        tree.collapsePath(path);
                        tree.repaint();
                    }
                }
            }
            tree.revalidate();
            m_controller.operationSelectDesignPart(node);
        }
}//GEN-LAST:event_treeMouseClicked

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt)//GEN-FIRST:event_treeValueChanged
    {//GEN-HEADEREND:event_treeValueChanged

        MyDefaultMutableTreeNode node = getLastSelectedPathComponent();
        m_controller.operationSelectDesignPart(node);
}//GEN-LAST:event_treeValueChanged

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed
        m_controller.operationClickOk();
        //formWindowClosed(null);
        dispose();
}//GEN-LAST:event_okButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        WindowListener[] listeners = getWindowListeners();
        for (int i = 0; i < listeners.length; i++) {
            this.removeWindowListener(listeners[i]);
        }
        setEnabled(false);
        dispose();
        invalidate();
        m_controller.operationCloseWindow();
    }//GEN-LAST:event_formWindowClosed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    private void selectAllCheckBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectAllCheckBoxActionPerformed
    {//GEN-HEADEREND:event_selectAllCheckBoxActionPerformed
        m_controller.selectAll(selectAllCheckBox.isSelected());
}//GEN-LAST:event_selectAllCheckBoxActionPerformed

    private void rbShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbShowAllActionPerformed
        m_controller.showAll(selectAllCheckBox.isSelected());
    }//GEN-LAST:event_rbShowAllActionPerformed

    private void rbSameParentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSameParentsActionPerformed
        m_controller.showSameParents(selectAllCheckBox.isSelected());
    }//GEN-LAST:event_rbSameParentsActionPerformed

    private MyDefaultMutableTreeNode getLastSelectedPathComponent() {
        return (MyDefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    }

    private boolean expandNode(JTree tree, TreePath parent, MyDefaultMutableTreeNode expand) {
        // Traverse children
        MyDefaultMutableTreeNode node = (MyDefaultMutableTreeNode) parent.getLastPathComponent();
        boolean expandThisNode = false;
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                MyDefaultMutableTreeNode n = (MyDefaultMutableTreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandThisNode = expandThisNode || expandNode(tree, path, expand);
            }
        }
        // Expansion or collapse must be done bottom-up
        if (expandThisNode) {
            tree.expandPath(parent);
        } else {
            //tree.collapsePath(parent);
        }
        if (node.equals(expand) || (expand.getParent().equals(node) && node.getChildAt(0)==expand)) {
            return true;
        } else {
            return expandThisNode;
        }
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(SelectController.TREE_LOAD)) {
            MyDefaultMutableTreeNode node = (MyDefaultMutableTreeNode) evt.getNewValue();
            m_jDesignTreeRoot.add(node);
        } else if (evt.getPropertyName().equals(SelectController.TREE_REFRESH)) {
            tree.updateUI();
        } else if (evt.getPropertyName().equals(SelectController.SELECT_DESIGN_PART)) {
        } else if (evt.getPropertyName().equals(SelectController.SHOW_SELECTER)) {
            this.centerParent();
        } else if (evt.getPropertyName().equals(SelectController.CHANGE_TITLE)) {
            setTitle(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(SelectController.CHANGE_SELECTED_NO)) {
            selectedNodeNumberLabel.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(SelectController.PROPERTIES)) {
            propertyPane.setText(evt.getNewValue().toString());
            propertyPane.repaint();
        } else if (evt.getPropertyName().equals(SelectController.EXPAND_PATH)) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            expandNode(tree, new TreePath(m_jDesignTreeRoot), node);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGrpFilter;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JTextPane propertyPane;
    private javax.swing.JScrollPane propertyScrollPane;
    private javax.swing.JRadioButton rbSameParents;
    private javax.swing.JRadioButton rbShowAll;
    private javax.swing.JPanel rightPanel;
    private javax.swing.JCheckBox selectAllCheckBox;
    private javax.swing.JLabel selectedNodeNumberLabel;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JTree tree;
    private javax.swing.JScrollPane treeScrollPane;
    // End of variables declaration//GEN-END:variables
}
